package servlets;

import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bean.AuctionItemBean;
import bean.UserBean;
import dao.AuctionItemDAO;
import dao.BidDAO;
import dao.UserDAO;
import database.DatabaseManager;

/**
 * Servlet implementation class AuctionTestServlet
 * 
 * servlet for testing purposes.
 * 
 * @author Sander Meijer & Stijn Kliemesch
 */
public class AuctionTestServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private UserBean sanderBean;
	private UserBean stijnBean;
	private UserBean henkBean;

	private AuctionItemBean boekBean;
	private AuctionItemBean huisjeBean;
	private AuctionItemBean beestjeBean;
	private AuctionItemBean paardBean;
	
	private AuctionItemBean balBean;
	private AuctionItemBean haarBean;
	private AuctionItemBean vliegtuigBean;
	private AuctionItemBean banaanBean;
	
	private AuctionItemBean vleugelsBean;
	private AuctionItemBean HenksVliegtuigBean;
	
	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AuctionTestServlet() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String action = request.getParameter("action");
		String passWord = request.getParameter("password");
		String allowed = request.getParameter("allowed");

		if (action != null && passWord != null && allowed != null) {
			if (action.equals("GOTO") && passWord.equals("MK11_AH_TEST")
					&& allowed.equals("TRUE")) {
				RequestDispatcher dispatcher = request
						.getRequestDispatcher(response
								.encodeURL("/WEB-INF/FillWithTestDataPage.html"));
				dispatcher.forward(request, response);
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		if (request.getParameter("Accept").equals("Ik ga akkoord")) {
			System.out.println("Filling Database with test data");
			System.out.println(request.getParameter("Accept"));

			DatabaseManager.getInstance().clear();

			insertUsers();
			insertAuctions();
			insertBids();

			RequestDispatcher dispatcher = request
					.getRequestDispatcher(response.encodeURL("/index.jsp"));
			dispatcher.forward(request, response);
		}
	}
	
	/**
	 * inserts many bids into the Database.
	 */
	private void insertBids(){
		System.out.println("\tinserting bids");
		BidDAO.insertBid(huisjeBean.getAuctionItemID(), stijnBean.getUserID(), 3449);
		BidDAO.insertBid(huisjeBean.getAuctionItemID(), henkBean.getUserID(), 3500);
		
		BidDAO.insertBid(beestjeBean.getAuctionItemID(), henkBean.getUserID(), 37);
		BidDAO.insertBid(beestjeBean.getAuctionItemID(), stijnBean.getUserID(), 40);
		BidDAO.insertBid(beestjeBean.getAuctionItemID(), henkBean.getUserID(), 80);
		
		BidDAO.insertBid(paardBean.getAuctionItemID(), stijnBean.getUserID(), 24);
		
		BidDAO.insertBid(balBean.getAuctionItemID(), sanderBean.getUserID(), 879);
		BidDAO.insertBid(balBean.getAuctionItemID(), henkBean.getUserID(), 888);
		
		BidDAO.insertBid(haarBean.getAuctionItemID(), sanderBean.getUserID(), 3);
		
		BidDAO.insertBid(banaanBean.getAuctionItemID(), sanderBean.getUserID(), 9);
		BidDAO.insertBid(banaanBean.getAuctionItemID(), henkBean.getUserID(), 10);
		BidDAO.insertBid(banaanBean.getAuctionItemID(), sanderBean.getUserID(), 11);
		BidDAO.insertBid(banaanBean.getAuctionItemID(), henkBean.getUserID(), 12);
		BidDAO.insertBid(banaanBean.getAuctionItemID(), sanderBean.getUserID(), 13);
		BidDAO.insertBid(banaanBean.getAuctionItemID(), henkBean.getUserID(), 14);
		BidDAO.insertBid(banaanBean.getAuctionItemID(), sanderBean.getUserID(), 15);
		BidDAO.insertBid(banaanBean.getAuctionItemID(), henkBean.getUserID(), 16);
		BidDAO.insertBid(banaanBean.getAuctionItemID(), henkBean.getUserID(), 17);
		BidDAO.insertBid(banaanBean.getAuctionItemID(), sanderBean.getUserID(), 18);
		BidDAO.insertBid(banaanBean.getAuctionItemID(), henkBean.getUserID(), 19);
		BidDAO.insertBid(banaanBean.getAuctionItemID(), sanderBean.getUserID(), 20);
		BidDAO.insertBid(banaanBean.getAuctionItemID(), henkBean.getUserID(), 21);
		BidDAO.insertBid(banaanBean.getAuctionItemID(), sanderBean.getUserID(), 205);
		
		BidDAO.insertBid(vleugelsBean.getAuctionItemID(), stijnBean.getUserID(), 78);
		
		BidDAO.insertBid(vliegtuigBean.getAuctionItemID(), henkBean.getUserID(), 700);
	}

	/**
	 * Inserts a couple of auctions in to the database.
	 */
	private void insertAuctions() {
		System.out.println("\tinserting auctions");
		AuctionItemDAO.insertNewAuction("Boek", 20, "Een boekje",
				sanderBean.getUserID());
		AuctionItemDAO.insertNewAuction("Paard en wagen", 41,
				"Een authentiek paard en wagen", sanderBean.getUserID());
		AuctionItemDAO
				.insertNewAuction(
						"Huisje, boompje en beestje",
						3447,
						"Een huis met in de tuin een boom. Ook staat er in de tuin een beestje, we weten niet precies wat van beestje.",
						sanderBean.getUserID());
		AuctionItemDAO.insertNewAuction("Beestje", 36, "Een beestje ofzo", sanderBean.getUserID());
		
		
		AuctionItemDAO.insertNewAuction("Voetbal", 874, "De voetbal van stijn",
				stijnBean.getUserID());
		AuctionItemDAO.insertNewAuction("Haar", 2, "Het haar van stijn", stijnBean.getUserID());
		AuctionItemDAO.insertNewAuction("Vliegtuig", 655, "Met vleugels en stuff", stijnBean.getUserID());
		AuctionItemDAO.insertNewAuction("Banaan", 7, "Geel en krom, niet recht", stijnBean.getUserID());
		
		AuctionItemDAO.insertNewAuction("Henk's vleugels", 63, "Ofzo iets", henkBean.getUserID());
		AuctionItemDAO.insertNewAuction("Henks's Vliegtuig", 655, "Met vleugels en stuff", henkBean.getUserID());

		ArrayList<AuctionItemBean> AuctionItemBeans = AuctionItemDAO
				.getSearchedAuctions("");

		for (AuctionItemBean aib : AuctionItemBeans) {
			if (aib.getTitle().equals("Boek")) {
				boekBean = aib;
			} else if (aib.getTitle().equals("Paard en wagen")) {
				paardBean = aib;
			} else if (aib.getTitle().equals("Huisje, boompje en beestje")) {
				huisjeBean = aib;
			}else if(aib.getTitle().equals("Beestje")){
				beestjeBean = aib;
			} else if (aib.getTitle().equals("Voetbal")) {
				balBean = aib;
			} else if (aib.getTitle().equals("Haar")) {
				haarBean = aib;
			} else if (aib.getTitle().equals("Vliegtuig")) {
				vliegtuigBean = aib;
			} else if (aib.getTitle().equals("Banaan")) {
				banaanBean = aib;
			} else if (aib.getTitle().equals("Henk's vleugels")) {
				vleugelsBean = aib;
			} else if (aib.getTitle().equals("Henks's Vliegtuig")) {
				HenksVliegtuigBean = aib;
			}
		}

		// lower the amount of time that is needed to for the auction to expire
		try {
			Timestamp fiveDays = new Timestamp(
					Calendar.getInstance().getTimeInMillis() + 432000000);
															   
			Timestamp fourminutes = new Timestamp(Calendar.getInstance().getTimeInMillis() + 240000);
			
			Timestamp someTimeAgo = new Timestamp(Calendar.getInstance().getTimeInMillis() - 240000);
			
			PreparedStatement psUpdateDate = DatabaseManager
					.getInstance()
					.getConnection()
					.prepareStatement(
							"UPDATE AuctionItem SET EndTime=? WHERE Title=?");
			psUpdateDate.setTimestamp(1, fourminutes);
			psUpdateDate.setString(2, boekBean.getTitle());
			psUpdateDate.execute();
			psUpdateDate.clearParameters();
			
			psUpdateDate.setTimestamp(1, fiveDays);
			psUpdateDate.setString(2, balBean.getTitle());
			psUpdateDate.execute();
			psUpdateDate.clearParameters();
			
			psUpdateDate.setTimestamp(1, fourminutes);
			psUpdateDate.setString(2, vliegtuigBean.getTitle());
			psUpdateDate.execute();
			psUpdateDate.clearParameters();
			
			psUpdateDate.setTimestamp(1, fiveDays);
			psUpdateDate.setString(2, HenksVliegtuigBean.getTitle());
			psUpdateDate.execute();
			psUpdateDate.clearParameters();
			
			psUpdateDate.setTimestamp(1, someTimeAgo);
			psUpdateDate.setString(2, beestjeBean.getTitle());
			psUpdateDate.execute();
			psUpdateDate.clearParameters();
			
			psUpdateDate.close();
						
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * inserts three users in to the database.
	 */
	private void insertUsers() {
		System.out.println("\tinserting users");
		UserDAO.createNewUser("Sander", "sander");
		sanderBean = UserDAO.getUserByUsername("Sander");

		UserDAO.createNewUser("Stijn", "stijn");
		stijnBean = UserDAO.getUserByUsername("Stijn");
		
		UserDAO.createNewUser("Henk", "henk");
		henkBean = UserDAO.getUserByUsername("Henk");
		
	}

}
